﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;

namespace ExFlash.Records
{
    //[TypeConverter(typeof(LineStyleArrayConverter))]
    public class LineStyleArray : ISwfSerializable, IList<LineStyleRecord>, IList
    {
        private readonly ShapeType _shapeType;

        public ushort LineStyleCount
        {
            get;
            private set;
        }

        public List<LineStyleRecord> LineStyles
        {
            get;
            private set;
        }

        public LineStyleArray(ShapeType type)
        {
            _shapeType = type;
        }

        #region ISwfSerializable Members

        public void SwfDeserialize(BinaryBitReader reader)
        {
            LineStyleCount = reader.ReadByte();
            
            if (LineStyleCount == 0xFF)
            {
                LineStyleCount = reader.ReadUInt16();
            }

            LineStyles = new List<LineStyleRecord>();

            if (_shapeType == ShapeType.Shape4)
            {
                for (int i = 0; i < LineStyleCount; i++)
                {
                    LineStyle2Record rec = new LineStyle2Record();
                    rec.SwfDeserialize(reader);
                    LineStyles.Add(rec);
                }
            }
            else
            {
                for (int i = 0; i < LineStyleCount; i++)
                {
                    LineStyleRecord rec = new LineStyleRecord(_shapeType);
                    rec.SwfDeserialize(reader);
                    LineStyles.Add(rec);
                }
            }
            
        }

        public void SwfSerialize(byte[] arr, ref int offset)
        {
            throw new NotImplementedException();
        }

        #endregion

        #region IList<LineStyleRecord> Members

        public int IndexOf(LineStyleRecord item)
        {
            return LineStyles.IndexOf(item);
        }

        public void Insert(int index, LineStyleRecord item)
        {
            throw new NotImplementedException();
        }

        public void RemoveAt(int index)
        {
            throw new NotImplementedException();
        }

        public LineStyleRecord this[int index]
        {
            get
            {
                return LineStyles[index];
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        #endregion

        #region ICollection<LineStyleRecord> Members

        public void Add(LineStyleRecord item)
        {
            throw new NotImplementedException();
        }

        public void Clear()
        {
            throw new NotImplementedException();
        }

        public bool Contains(LineStyleRecord item)
        {
            return LineStyles.Contains(item);
        }

        public void CopyTo(LineStyleRecord[] array, int arrayIndex)
        {
            LineStyles.CopyTo(array, arrayIndex);
        }

        public int Count
        {
            get { return LineStyleCount; }
        }

        public bool IsReadOnly
        {
            get { return true; }
        }

        public bool Remove(LineStyleRecord item)
        {
            throw new NotImplementedException();
        }

        #endregion

        #region IEnumerable<FillStyleRecord> Members

        public IEnumerator<LineStyleRecord> GetEnumerator()
        {
            return LineStyles.GetEnumerator();
        }

        #endregion

        #region IEnumerable Members

        IEnumerator IEnumerable.GetEnumerator()
        {
            IEnumerable temp = LineStyles;
            return temp.GetEnumerator();
        }

        #endregion

        #region IList Members

        int IList.Add(object value)
        {
            throw new NotImplementedException();
        }

        void IList.Clear()
        {
            throw new NotImplementedException();
        }

        bool IList.Contains(object value)
        {
            return Contains((LineStyleRecord)value);
        }

        int IList.IndexOf(object value)
        {
            return IndexOf((LineStyleRecord)value);
        }

        void IList.Insert(int index, object value)
        {
            throw new NotImplementedException();
        }

        bool IList.IsFixedSize
        {
            get { return true; }
        }

        bool IList.IsReadOnly
        {
            get { return true; }
        }

        void IList.Remove(object value)
        {
            throw new NotImplementedException();
        }

        void IList.RemoveAt(int index)
        {
            throw new NotImplementedException();
        }

        object IList.this[int index]
        {
            get
            {
                return LineStyles[index];
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        #endregion

        #region ICollection Members

        void ICollection.CopyTo(Array array, int index)
        {
            CopyTo((LineStyleRecord[])array, index);
        }

        int ICollection.Count
        {
            get { return Count; }
        }

        bool ICollection.IsSynchronized
        {
            get { return false; }
        }

        object ICollection.SyncRoot
        {
            get { throw new NotImplementedException(); }
        }

        #endregion
    }

    public class LineStyleArrayConverter : CollectionConverter
    {

    }
}
